package com.salton123.colorflydemo.ui.aty

import android.content.Context
import android.os.Bundle
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.salton123.C
import com.salton123.bmob.biz.mandala.bean.MandalaItem
import com.salton123.colorflydemo.R
import com.salton123.ui.base.BaseActivity
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.aty_mandala_paint_board.*
import java.io.File

/**
 * User: newSalton@outlook.com
 * Date: 2019/6/3 17:19
 * ModifyTime: 17:19
 * Description:
 */
class MandalaPaintBoardAty : BaseActivity() {
    private var mandalaItem: MandalaItem? = null

    override fun initVariable(savedInstanceState: Bundle?) {
        mandalaItem = intent.getParcelableExtra(C.ARG_ITEM)
    }

    override fun initViewAndData() {
        Observable.create(ObservableOnSubscribe<File> { emitter ->

            val mRequestBuilder = Glide.with(activity()).downloadOnly()
            mRequestBuilder.load(mandalaItem?.url)
            mRequestBuilder.listener(object : RequestListener<File> {
                override fun onLoadFailed(
                        e: GlideException?,
                        model: Any,
                        target: Target<File>,
                        isFirstResource: Boolean): Boolean {
                    emitter.onError(Exception("onLoadFailed"))
                    return false
                }

                override fun onResourceReady(
                        resource: File,
                        model: Any,
                        target: Target<File>,
                        dataSource: DataSource,
                        isFirstResource: Boolean): Boolean {
                    emitter.onNext(resource)
                    return false
                }
            })
            mRequestBuilder.preload()
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    mMandalaGestureView.loadImage(it.absolutePath)
                }, { e -> e.printStackTrace() }, {})
        ivSave.setOnClickListener {
            val result = mMandalaGestureView.saveBitmap("/sdcard/mandala/" + System.currentTimeMillis() + ".png")
            if (result) {
                shortToast("保存成功")
            } else {
                shortToast("保存失败")
            }
        }
    }

    fun test(context: Context, url: String) {
        val mRequestBuilder = Glide.with(context).downloadOnly()
        mRequestBuilder.load(url)
        mRequestBuilder.listener(object : RequestListener<File> {
            override fun onLoadFailed(
                    e: GlideException?,
                    model: Any,
                    target: Target<File>,
                    isFirstResource: Boolean): Boolean {
                return false
            }

            override fun onResourceReady(
                    resource: File,
                    model: Any,
                    target: Target<File>,
                    dataSource: DataSource,
                    isFirstResource: Boolean): Boolean {
                return false
            }
        })
        mRequestBuilder.preload()
    }

    override fun getLayout(): Int = R.layout.aty_mandala_paint_board

}